package com.lenovo.smarttraffic.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.bean.CarInfo;
import com.lenovo.smarttraffic.bean.ChargeHistory;
import com.lenovo.smarttraffic.bean.UserBean;

public class ETCChargeHistoryAdapter extends BaseAdapter {

    private ChargeHistory chargeHistory;
    private CarInfo account;
    private UserBean user;

    private Context context;

    public ETCChargeHistoryAdapter(Context context, ChargeHistory chargeHistory, UserBean user, CarInfo account) {
        this.context = context;
        this.chargeHistory = chargeHistory;
        this.user = user;
        this.account = account;
    }

    @Override
    public int getCount() {
        return chargeHistory.getROWS_DETAIL().size();
    }

    @Override
    public Object getItem(int position) {
        return chargeHistory.getROWS_DETAIL().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.list_item_etc_charge_history, parent, false);
            holder = new ViewHolder();
            holder.time = convertView.findViewById(R.id.time);
            holder.title = convertView.findViewById(R.id.title);
            holder.value = convertView.findViewById(R.id.value);
            holder.money = convertView.findViewById(R.id.money);
            holder.charge_time = convertView.findViewById(R.id.charge_time);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (chargeHistory.getROWS_DETAIL() != null && chargeHistory.getROWS_DETAIL().size() > 0
                && user.getROWS_DETAIL() != null && user.getROWS_DETAIL().size() > 0
                && account.getROWS_DETAIL() != null && account.getROWS_DETAIL().size() > 0) {

            ChargeHistory.ROWSDETAILBean chargeHistoryBean = chargeHistory.getROWS_DETAIL().get(position);
            UserBean.ROWS_DETAIL userBean = user.getROWS_DETAIL().get(position);
            CarInfo.ROWSDETAILBean carInfoBean = account.getROWS_DETAIL().get(position);

            String time = chargeHistoryBean.getChargetime();
            if (time == null) {
                holder.time.setText("充值时间为空");
                holder.charge_time.setText("充值时间为空");
            } else {
                holder.time.setText(time);
                holder.charge_time.setText(time);
            }

            holder.title.setText(String.format("充值人：%s", userBean.getPname()));

            holder.value.setText(String.format("车牌号：%s", carInfoBean.getCarnumber()));

            holder.money.setText(String.format("充值：%s元", chargeHistoryBean.getMoney()));

        } else {
            holder.money.setText("暂无充值历史记录");
        }

        return convertView;
    }

    static class ViewHolder {
        TextView time, title, value, money, charge_time;
    }
}
